package com.dycx.common;

import java.util.concurrent.atomic.AtomicInteger;

public class CounterLimiter {
    private int limit;
    private AtomicInteger counter;

    public CounterLimiter(int limit) {
        this.limit = limit;
        this.counter = new AtomicInteger(0);
    }

    public boolean allowRequest() {
        int currentCount = counter.incrementAndGet();
        if (currentCount > limit) {
            counter.decrementAndGet();
            return false;
        }
        return true;
    }

    public int getLimit() {
        return this.limit;
    }

    public int getCounter() {
        return this.counter.get();
    }
}